<template>
  <div class="articleList" ref="listBox" @scroll="onScroll">
<van-pull-refresh v-model="isLoading" @refresh="onRefresh">
<van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell @click="$router.push({ path: '/news', query: { id: item.art_id } })" v-for="(item, index) in list" :key="index">
        <div>{{ item.title }}</div>
        <van-grid :border="false" :column-num="item.cover.type">
          <van-grid-item v-for="(value, idx) in item.cover.images" :key="idx">
            <van-image :src="value" />
          </van-grid-item>
        </van-grid>
        <div class="author">
          <div class="comment">
            <span>{{ item.aut_name }}</span>
            <span>{{ item.comm_count + "评论" }}</span>
            <span>{{ item.pubdate |rlvTime}}</span>
          </div>
          <van-button class="fork" icon="cross" />
        </div>
      </van-cell>
    </van-list>
</van-pull-refresh>
  </div>
</template>

<script>
import { articleListAPI } from "@/api";
import { login } from "@/api/user";
export default {
  props: ["channel_id"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      isLoading: false,
    };
  },
  // 当他重新被显示调用的钩子
  activated() {
    this.$refs.listBox.scrollTop = this.scTop;
  },
  methods: {
    // 滚动触发的事件
    onScroll() {
      // 拿到滚动的距离，保存起来
      this.scTop = this.$refs.listBox.scrollTop;
    },
    async onLoad() {
      let res = await articleListAPI({
        channel_id: this.channel_id,
        timestamp: this.timestamp,
      });
      this.list.push(...res.data.data.results);
      this.timestamp = res.data.data.pre_timestamp;
      this.loading = false;
      if (res.data.data.pre_timestamp == null) {
        this.finished = true;
      }
    },
    async onRefresh() {
      let res = await articleListAPI({
        channel_id: this.channel_id,
        timestamp: this.timestamp,
      });
      this.list = res.data.data.results;
      this.isLoading = false;
    },
  },
};
</script>

<style lang='less' scope>
.articleList {
  position: fixed;
  top: 90px;
  bottom: 50px;
  overflow: auto;
  width: 100vw;
}
.author {
  // height: 20px;
  color: #999899;
  font-size: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
  .comment {
    flex: 1;
    span {
      margin-right: 8px;
    }
  }
  .fork {
    border: none;
    width: 14px;
    color: #999899;
  }
}
</style>